package com.dense.kuiniu.floatcabinet.mvp.model;

import com.dense.kuiniu.floatcabinet.mvp.contract.BusinessContract;
import com.dense.kuiniu.floatcabinet.mvp.http.api.service.CommonService;
import com.dense.kuiniu.floatcabinet.mvp.http.entity.BaseResponse;
import com.dense.kuiniu.floatcabinet.mvp.http.entity.Book;
import com.dense.kuiniu.floatcabinet.mvp.http.entity.BorrowBackResult;
import com.dense.kuiniu.floatcabinet.mvp.http.entity.Inventory;
import com.dense.kuiniu.floatcabinet.mvp.http.entity.Reader;
import com.dense.kuiniu.floatcabinet.mvp.http.post.BorrowOrBackBody;
import com.dense.kuiniu.floatcabinet.mvp.http.post.InventoryBody;
import com.jess.arms.integration.IRepositoryManager;
import com.jess.arms.mvp.BaseModel;

import java.util.List;

import io.reactivex.Observable;

/**
 * @author Administrator
 * @date 2020/11/11
 */
public class BusinessModel extends BaseModel implements BusinessContract.Model {
    public BusinessModel(IRepositoryManager repositoryManager) {
        super(repositoryManager);
    }

    @Override
    public Observable<BorrowBackResult> borrowOrBack(BorrowOrBackBody borrow, BorrowOrBackBody back) {
        Observable<BaseResponse<Reader>> borrowResult = mRepositoryManager.obtainRetrofitService(CommonService.class)
                .borrow(borrow);
        Observable<BaseResponse<List<Book>>> backResult = mRepositoryManager.obtainRetrofitService(CommonService.class)
                .back(back);
        return Observable.zip(borrowResult, backResult, (readResp, backResp) -> {
            BorrowBackResult result = new BorrowBackResult();
            if (readResp.isSuccess()) {
                Reader reader = readResp.getData();
                result.setBorrowList(reader.getBookList());
            }
            if (backResp.isSuccess()) {
                result.setBackList(backResp.getData());
            }
            return result;
        });
    }

    @Override
    public Observable<BaseResponse<Reader>> borrow(BorrowOrBackBody borrow) {
        return mRepositoryManager.obtainRetrofitService(CommonService.class)
                .borrow(borrow);
    }

    @Override
    public Observable<BaseResponse<List<Book>>> back(BorrowOrBackBody borrow) {
        return mRepositoryManager.obtainRetrofitService(CommonService.class)
                .back(borrow);
    }

    @Override
    public Observable<BaseResponse<Inventory>> inventory(InventoryBody body) {
        return mRepositoryManager.obtainRetrofitService(CommonService.class)
                .inventory(body);
    }

    @Override
    public void saveInventory(Inventory inventory) {

    }


}